package com.love_weinuo.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.love_weinuo.R;
import com.love_weinuo.bean.LandListCallBackBean;
import com.love_weinuo.databinding.ItemMyLandBinding;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.dialog.InCommonDialog;
import com.love_weinuo.util.listener.OnBHClickListener;
import com.love_weinuo.util.listener.OnDeleteClickListener;
import com.love_weinuo.util.listener.OnPindanPointListener;

import java.util.List;

/**
 * Created by SensYang on 2019/03/01 10:21
 */

public class MyLandAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<LandListCallBackBean.ResultEntity.ResEntity> strings;
    private Context mcontext;
    OnDeleteClickListener onLonelyListener;
    OnPindanPointListener onPindanPointListener;

    public void setOnPindanPointListener(OnPindanPointListener onPindanPointListener){
        this.onPindanPointListener = onPindanPointListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener listener) {
        this.onLonelyListener = listener;
    }

    public MyLandAdapter(Context context, List<LandListCallBackBean.ResultEntity.ResEntity> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemMyLandBinding functionBinding = DataBindingUtil.inflate(LayoutInflater.from(mcontext), R.layout.item_my_land, parent, false);
        functionBinding.setAdapter(this);
        return new TourViewHolder(functionBinding);
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        TourViewHolder tv = (TourViewHolder) holder;
        tv.moduleDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLonelyListener != null) {
                    onLonelyListener.setOnDeleteClickListener(position);
                }
            }
        });

        if (!"0".equals(strings.get(position).getState())) {
            LogUtil.e("使用的土地" + strings.get(position).getName() + "  " + strings.get(position).getState());
            tv.tvModuleState.setVisibility(View.VISIBLE);
        }

        tv.tvModulePerimeter.setText(strings.get(position).getPerimeter() + "");
        tv.tvMoudleLandarea.setText(strings.get(position).getLandarea() + "");


        tv.llModuleDesc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onPindanPointListener != null) {
                    onPindanPointListener.setOnPindanPointListener(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<LandListCallBackBean.ResultEntity.ResEntity> {
        ItemMyLandBinding functionBinding;
        LinearLayout moduleDelete;
        LinearLayout llModuleDesc;
        TextView tvModuleState,tvModulePerimeter,tvMoudleLandarea;

        public TourViewHolder(ItemMyLandBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            moduleDelete = dataBinding.moduleDelete;
            llModuleDesc = dataBinding.llModuleDesc;
            tvModuleState = dataBinding.tvModuleState;
            tvModulePerimeter = dataBinding.tvModulePerimeter;
            tvMoudleLandarea = dataBinding.tvMoudleLandarea;
        }

        @Override
        public void bindData(LandListCallBackBean.ResultEntity.ResEntity data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }


    public void toNext(View view, final LandListCallBackBean.ResultEntity.ResEntity t) {
        InCommonDialog dialog = new InCommonDialog(mcontext);
        dialog.setTitleText("确认删除" + t.getName() + "?");
        dialog.show();
        dialog.setOnBHClickListener(new OnBHClickListener() {
            @Override
            public void onConfirmClick() {

            }

            @Override
            public void onCloseClick() {

            }
        });
    }


}